﻿using Share;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.EventSystems;

public static partial class UITools
{


    public static void SaveImageToPng(Sprite sprite, string path)
    {
        Texture2D texture = sprite.texture;

        // 创建一个新的 Texture2D 并复制 Sprite 的像素数据
        Texture2D newTexture = new Texture2D(texture.width, texture.height, texture.format, false);
        newTexture.SetPixels(texture.GetPixels());

        newTexture.Apply();

        // 将 Texture2D 编码为 PNG
        byte[] bytes = texture.EncodeToPNG();

        // 保存 PNG 文件
        File.WriteAllBytes(path, bytes);
    }

    /// <summary>
    /// 获取png图片的真实尺寸
    /// </summary>
    /// <param name="filePath"></param>
    /// <param name="width"></param>
    /// <param name="height"></param>
    public static void GetPNGSize(string filePath, out int width, out int height)
    {
        byte[] fileData = File.ReadAllBytes(filePath);
        Texture2D texture = new Texture2D(2, 2);
        texture.LoadImage(fileData);
        width = texture.width;
        height = texture.height;
    }

    /// <summary>
    /// 设置SpriteRenderer的颜色为黑色，并调整透明度
    /// </summary>
    /// <param name="sp">SpriteRenderer对象</param>
    /// <param name="colorV">颜色值</param>
    /// <param name="a">透明度</param>
    public static void SetSpriteRenderBlack(GameObject sp, float colorV, float a)
    {
        if (sp == null)
            return;
        SpriteRenderer ren = sp.GetComponent<SpriteRenderer>();
        float h;
        float s;
        float v;
        Color.RGBToHSV(ren.color, out h, out s, out v);
        Color c = Color.HSVToRGB(h, s, colorV);
        c.a = a;
        ren.color = c;
    }

    /// <summary>
    /// 获取屏幕宽度缩放比例
    /// </summary>
    /// <returns>屏幕宽度缩放比例</returns>
    public static float GetScreenScaleWidth(float width = 2560)
    {
        return (float)Screen.width / width;
    }

    /// <summary>
    /// 获取屏幕高度缩放比例
    /// </summary>
    /// <returns>屏幕高度缩放比例</returns>
    public static float GetScreenScaleHeight(float height = 1440)
    {
        return (float)Screen.height / height;
    }

    /// <summary>
    /// 检查指定的点是不是在某个tag叫做 strLayerName的界面上
    /// </summary>
    /// <param name="pos">点的位置</param>
    /// <param name="strLayerName">层的名称</param>
    /// <returns>如果点在指定的层上返回true，否则返回false</returns>
    public static bool IsPositionOverLayer(Vector2 pos, string strLayerName)
    {
        List<RaycastResult> results = new List<RaycastResult>();
        PointerEventData eventData = new PointerEventData(EventSystem.current);
        eventData.position = new Vector2(pos.x, pos.y);
        EventSystem.current.RaycastAll(eventData, results);
        for (int i = 0; i < results.Count; i++)
        {
            if (results[i].gameObject.CompareTag(strLayerName))
            {
                return true;
            }
        }

        return false;
    }

    public static Sprite LoadSprite(string _path, bool dontDestory = false)
    {
        var result = ResourceLoader.Instance.LoadAsset<Sprite>(_path, dontDestory);

        if (result == null)
        {
            XKLog.LogError("没有找到[{0}]图片", _path);
            return null;
        }

        return result;
    }

}
